package com.example.studentcoursesysteam.Controller;

import com.example.studentcoursesysteam.Bean.StudentCourse;
import com.example.studentcoursesysteam.Repository.CourseRepository;
import com.example.studentcoursesysteam.Repository.StudentCourseRepository;
import com.example.studentcoursesysteam.Repository.StudentRepository;
import com.example.studentcoursesysteam.Repository.TeacherRepository;
import com.example.studentcoursesysteam.Vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/studentCourse")
public class StudentCourseController {
    @Autowired
    StudentRepository studentRepository;
    @Autowired
    CourseRepository courseRepository;
    @Autowired
    StudentCourseRepository studentCourseRepository;
    @Autowired
    TeacherRepository teacherRepository;
    @RequestMapping("/insertStudentCourse")
    public String insertStudentCourse(StudentCourse studentCourse){
        if (!studentCourseRepository.existsByStudentCodeAndCourseCode(studentCourse.getStudentCode(),studentCourse.getCourseCode())){
            studentCourseRepository.save(studentCourse);
            return "添加选课成功";
        }else
            return "学生已选该课，选课失败";
    }

    @RequestMapping("/deleteStudentCourse")
    public String deleteStudentCourse( String studentCode,String courseCode){
        if (studentCourseRepository.existsByStudentCodeAndCourseCode(studentCode,courseCode)){
            studentCourseRepository.deleteCourse(studentCode,courseCode);
            return "删除成功";
        }else
            return "选课不存在，删除失败";

    }

    @RequestMapping("/deleteStudentCourseAPI")
    public String deleteStudentCourseAPI( String studentCode){
        if (studentCourseRepository.existsByStudentCode(studentCode)){
            studentCourseRepository.deleteStudentId(studentCode);
            return "删除成功";
        }else
            return "学生不存在，删除失败";

    }

    @RequestMapping("/updateAchievement")
    public String updateAchievement( Float achievement,String studentCode,String courseCode){
        studentCourseRepository.updateStatus(achievement,studentCode,courseCode);
        return "修改成功";
    }

    @RequestMapping("/selectStudentCourse1")
    public List<StudentCourseVo> selectStudentCourse1(String studentCode) {
        List<StudentCourseVo> studentCourseVoList=studentCourseRepository.selectStudentCourse(studentCode);
        return studentCourseVoList;
    }

    @RequestMapping("/selectAchievement")
    public List<AchievementVo> selectAchievement(String studentCode){
        return studentCourseRepository.selectAchievement(studentCode);
    }

    @RequestMapping("/selectCAchievement")
    public List<StudentAchievementVo> selectCAchievement(String studentCode){
        return studentCourseRepository.selectCAchievement(studentCode);
    }

    @RequestMapping("/selectStudentCourseName")
    public Page<StudentCourseNameVo> selectStudentCourseName(String name) {
        Pageable pageable = PageRequest.of(0, 20);
        Page<StudentCourseNameVo> studentCourseVoList=studentCourseRepository.selectStudentCourseName(name,pageable);
        return studentCourseVoList;
    }

    @RequestMapping("/selectStudentCourseAchievement")
    public List<CourseMaxMinAvgVo> selectStudentCourseAchievement(String name) {
        List<CourseMaxMinAvgVo> studentCourseVoList=studentCourseRepository.selectStudentCourseAchievement(name);
        return studentCourseVoList;
    }

    @RequestMapping("/selectStudentCourseAchievementCode")
    public List<CourseMaxMinAvgVo> selectStudentCourseAchievementCode(String studentCode) {
        List<CourseMaxMinAvgVo> studentCourseVoList=studentCourseRepository.selectStudentCourseAchievementCode(studentCode);
        return studentCourseVoList;
    }

    @RequestMapping("/selectStudentCourse")
    public List<StudentCourse> selectStudentCourse(String studentCode) {
        List<StudentCourse> studentCourseVoList=studentCourseRepository.findByStudentCode(studentCode);
        return studentCourseVoList;
    }
}
